﻿using System;
using System.Collections.Generic;
using System.Text;
using SAServices.DAL;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace SAServices.BLL
{
    public class FamilyAttrBLL
    {
        private static int thumbnailHeight = (int)(40*2);
        private static int thumbnailWidth = (int)(30*2);
        private static bool ThumbnailCallback() { return false; }

        public static byte[] ResizeImageFile(byte[] imageFile, int targetSizeW, int targetSizeH)
        {
            System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
            int targetH, targetW;
            targetW = targetSizeW;
            targetH = (int)(original.Height * ((float)targetSizeW / (float)original.Width));
            if (targetH > targetSizeH)
            {
                targetH = targetSizeH;
                targetW = (int)(original.Width * ((float)targetSizeH / (float)original.Height));
            }
            if (targetSizeW < (int)original.Width || targetSizeH < (int)original.Height)
            {
                System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile));
                // Create a new blank canvas.  The resized image will be drawn on this canvas.
                Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(72, 72);
                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.PixelOffsetMode = PixelOffsetMode.HighQuality;
                grPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, targetW, targetH), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel);
                // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
                MemoryStream mm = new MemoryStream();
                bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                original.Dispose();
                imgPhoto.Dispose();
                bmPhoto.Dispose();
                grPhoto.Dispose();
                return mm.GetBuffer();
            }
            else
            {
                return imageFile;
            }
        }

        internal static bool AddFamilyAttrInfo(Model.ConnectionConext connCtx, Model.FamilyAttrInfo familyAttrInfo)
        {
            //generate thumbnail from modelPicBinary.
            MemoryStream thumbnailBinary = new MemoryStream();

            if (familyAttrInfo.ModelPicBinary.Length > 0)
            {
                try
                {
                    Image thumbnailImg = Image.FromStream(new MemoryStream(familyAttrInfo.ModelPicBinary)).GetThumbnailImage(thumbnailHeight, thumbnailWidth, ThumbnailCallback, IntPtr.Zero);
                    thumbnailImg.Save(thumbnailBinary, System.Drawing.Imaging.ImageFormat.Jpeg);
                    familyAttrInfo.ModelPicThumbnailBinary = thumbnailBinary.ToArray();
                }
                catch (System.Exception ex)
                {
                    familyAttrInfo.ModelPicThumbnailBinary = familyAttrInfo.ModelPicBinary;
                }
                
               // familyAttrInfo.ModelPicThumbnailBinary = ResizeImageFile(familyAttrInfo.ModelPicBinary, thumbnailHeight, thumbnailWidth);
            }
            else
                familyAttrInfo.ModelPicThumbnailBinary = familyAttrInfo.ModelPicBinary;

            return  FamilyAttrDAL.AddFamilyAttrInfo(connCtx, familyAttrInfo);
        }

        internal static bool UpdateFamilyAttrInfo(Model.ConnectionConext connCtx, Model.FamilyAttrInfo familyAttrInfo)
        {
            //generate thumbnail from modelPicBinary.
            MemoryStream thumbnailBinary = new MemoryStream();

            if (familyAttrInfo.ModelPicBinary.Length > 0)
            {
               try
               {
                   Image thumbnailImg = Image.FromStream(new MemoryStream(familyAttrInfo.ModelPicBinary)).GetThumbnailImage(
                    thumbnailHeight, thumbnailWidth, ThumbnailCallback, IntPtr.Zero);
                   thumbnailImg.Save(thumbnailBinary, System.Drawing.Imaging.ImageFormat.Jpeg);
                   familyAttrInfo.ModelPicThumbnailBinary = thumbnailBinary.ToArray();
               }
               catch (System.Exception ex)
               {
                   familyAttrInfo.ModelPicThumbnailBinary = familyAttrInfo.ModelPicBinary;
               }

                
               // familyAttrInfo.ModelPicThumbnailBinary = ResizeImageFile(familyAttrInfo.ModelPicBinary, thumbnailHeight, thumbnailWidth);
            }
            else
                familyAttrInfo.ModelPicThumbnailBinary = familyAttrInfo.ModelPicBinary;

            return FamilyAttrDAL.UpdateFamilyAttrInfo(connCtx, familyAttrInfo);
        }

        internal static bool DeleteFamilyAttrInfo(Model.ConnectionConext connCtx, Guid familyId)
        {
            return FamilyAttrDAL.DeleteFamilyAttrInfo(connCtx, familyId);
        }

        internal static Model.FamilyAttrInfo GetFamilyAttrInfo(Model.ConnectionConext connCtx, Guid familyId, Model.FamilyAttrType familyAttrType)
        {
            return FamilyAttrDAL.GetFamilyAttrInfo(connCtx, familyId, familyAttrType);
        }

        internal static Model.FamilyAttrInfo GetFamilyAttrInfo(Model.ConnectionConext connCtx, Guid familyId)
        {
            return FamilyAttrDAL.GetFamilyAttrInfo(connCtx, familyId);
        }

        internal static byte[] GetFamilyThumbnailPic(Model.ConnectionConext connCtx, Guid familyId)
        {
            return FamilyAttrDAL.GetFamilyThumbnailPic(connCtx, familyId);
        }
    }
}
